<?php


namespace app\admin\controller\finance;

use app\common\controller\Backend;
use think\Db;
use app\admin\model\user\User;
class Order extends Backend
{

    /**
     * Index模型对象
     * @var \app\admin\model\finance\Order
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\finance\Order;
    }


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

//            $list = Db::name('user')
////                ->where('is_vip',1)
//                ->field('id,nickname,avatar')
//                ->order('id desc')
//                ->paginate($limit)->each(function($item,$key){
//                    // 余额
//                    $item['yue'] = Db::name('order')->where(['user_id'=>$item['id'],'type'=>1])->order('id desc')->sum('money');
//                    // 微信
//                    $item['wx'] = Db::name('order')->where(['user_id'=>$item['id'],'type'=>2])->order('id desc')->sum('money');
//                    // 积分
//                    $item['int'] = Db::name('order')->where(['user_id'=>$item['id'],'type'=>3])->order('id desc')->sum('money');
//                    $createtime  = Db::name('order')->where(['user_id'=>$item['id']])->value('create_time');
//                    if(!$createtime)
//                    {
//                        $createtime = date('Y-m-d H:i:s',time());
//                    }
//                    $item['createtime'] = $createtime;
//                    return $item;
//                });

            $list = Db::name('user')
                ->where($where)
//                ->where('is_vip',1)
                ->field('id,nickname,avatar,createtime')
                ->order('id desc')
                ->paginate($limit)->toArray();
            
            foreach($list['data'] as &$val)
            {
                // // 余额
                // $val['yue'] = Db::name('order')->where(['user_id'=>$val['id'],'type'=>1])->where($where)->order('id desc')->sum('money');
                // // 微信
                // $val['wx'] = Db::name('order')->where(['user_id'=>$val['id'],'type'=>2])->where($where)->order('id desc')->sum('money');
                // // 积分
                // $val['int'] = Db::name('order')->where(['user_id'=>$val['id'],'type'=>3])->where($where)->order('id desc')->sum('money');
                // $createtime  = Db::name('order')->where(['user_id'=>$val['id']])->value('create_time');
                
                // 余额
                $val['yue'] = Db::name('order')->where(['user_id'=>$val['id'],'type'=>1])->order('id desc')->sum('money');
                // 微信
                $val['wx'] = Db::name('order')->where(['user_id'=>$val['id'],'type'=>2])->order('id desc')->sum('money');
                // 积分
                $val['int'] = Db::name('order')->where(['user_id'=>$val['id'],'type'=>3])->order('id desc')->sum('money');
                
                $createtime  = Db::name('order')->where(['user_id'=>$val['id']])->value('create_time');
                if(!$createtime)
                {
                    $val['create_time'] = date('Y-m-d H:i:s',time());
                }else{
                    $val['create_time'] = date('Y-m-d H:i:s',$createtime);
                }
            }

            $num_count = array_column($list['data'],'create_time');//返回数组中指定的一列
            array_multisort($num_count,SORT_DESC,$list['data']);//对多个数组或多维数组进行排序

            $result = array("total" => $list['total'], "rows" => $list['data']);
//            $result = array("total" => $list->total(), "rows" => $list->items());
            
            return json($result);
        }
        return $this->view->fetch();
    }

    /*
     * 余额订单明细
     * */
    public function info($ids)
    {
        $row =
        $row = $this->model->get(['id' => $ids]);
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $list =Db::name('user_consume')
            ->where(['user_id'=>$ids,'type'=>1])
            ->order('id desc')
            ->paginate(10,false,['query'=>request()->param()])->each(function ($item,$key){
                $item['create_time'] =  date('Y-m-d H:i',$item['create_time']);
                if ($item['status']==2){
                    $item['money'] = '-'.$item['money'];
                }
                return $item;
            });
        $this->view->assign("row",$list);
        return $this->view->fetch();
    }


    /*
     * 微信订单明细
     * */
    public function infoWx($ids)
    {
        $row = $this->model->get(['id' => $ids]);


        list($where, $sort, $order, $offset, $limit) = $this->buildparams();

        $list =Db::name('user_consume')
            ->where(['user_id'=>$ids,'type'=>4,'state'=>1])
            ->order('id desc')
            ->paginate(10,false,['query'=>request()->param()])->each(function ($item,$key){
                $item['create_time'] =  date('Y-m-d H:i',$item['create_time']);
                $item['type_name'] = '购买商品';
                return $item;
            });



        $this->view->assign("row",$list);
        return $this->view->fetch();
    }


    /*
     * 积分订单明细
     * */
    public function infoInt($ids)
    {
        $row = $this->model->get(['id' => $ids]);


        list($where, $sort, $order, $offset, $limit) = $this->buildparams();

        $list =Db::name('order')
            ->where(['user_id'=>$ids,'type'=>3])
//            ->where('status','in','5,6')
            ->limit($limit)
            ->order('id desc')
            ->select();

        $page =Db::name('order')
            ->where('user_id',$ids)
//            ->where('status','in','5,6')
            ->order('id desc')
            ->paginate($limit);

        if($list)
        {
            foreach($list as &$val)
            {
                switch($val['type'])
                {
                    case 1;
                        $val['type'] = '金额';
                        break;
                    case 2;
                        $val['type'] = '微信支付';
                        break;
                    case 3;
                        $val['type'] = '积分';
                        break;
                }
                $val['money']       = '-'.$val['money'];
                $val['create_time'] = date('Y-m-d H:i',$val['create_time']);
            }
        }

        $this->view->assign("row",$list);
        $this->view->assign("page",$page);
        return $this->view->fetch();
    }
}